<?php
namespace Portal\Controller;
use Common\Controller\AdminbaseController;


class AdminStatisController extends AdminbaseController {
	function _initialize() {
        //print_r($_SERVER['SCRIPT_URI']);
	}
    function index(){
        $api_obj = M('Api');
        $result = $api_obj->order('uri ASC')->field('uri,freq')->select();
        $freq = array();
        $api = array();
        $data = "[";
        foreach ($result as $r){
            array_push($freq,$r['freq']);
            array_push($api,$r['uri']);
            $data .= "{value:".$r['freq'].",name:'".$r['uri']."'},";
        }
        $data.="]";
        $this->assign("times",$this->join_jsarr($freq,1));
        $this->assign("api",$this->join_jsarr($api));
        $this->assign("data",$data);

        $this->display();
    }
	function trend(){
        $pid = $_GET['postid'];
        $stat_model = M('Statistic');
        $result = $stat_model->join('sp_posts ON sp_statistic.post_id = sp_posts.id')->where('post_id = '.$pid)->order('date asc')->field('post_id,post_title,date,timelen,hit')->select();
        //print_r($result);
        $hit=array();
        $timeval=array();
        $date=array();
        $i = 0;
        foreach ($result as $r) {
            if ($i == 0) {
               $_date = $r['date'];
               $title = $r['post_title'];
            } else {
               $_date = date("Y-m-d",strtotime("$_date +1 day"));
                while ($r['date']!=$_date) {
                     array_push($hit,0);
                     array_push($timeval,0);
                     array_push($date,$_date);
                     $_date = date("Y-m-d",strtotime("$_date +1  day"));
                }
            }
            $i++;
            array_push($hit,$r['hit']);
            array_push($timeval,$r['timelen']);
            array_push($date,$r['date']);
        }
        $this->assign("hit",$this->join_jsarr($hit,1));
        $this->assign("timeval",$this->join_jsarr($timeval,1));
        $this->assign("date",$this->join_jsarr($date));
        $this->assign("title",$title);
        $this->display();
	}
    private function join_jsarr($arr,$type=0){
        if ($type == 0) {
            $arr = join("','",$arr);
            $arr = "['".$arr."']";
        } else {
            $arr = join(",",$arr);
            $arr = "[".$arr."]";
        }
        
        return $arr;
    }
    function timeval(){
        require_once(LIB_PATH.'Vendor/jpgraph/jpgraph.php');
        require_once(LIB_PATH.'Vendor/jpgraph/jpgraph_line.php');
        $pid = $_GET['postid'];
        $stat_model = M('Statistic');
        $result = $stat_model->join('sp_posts ON sp_statistic.post_id = sp_posts.id')->where('post_id = '.$pid)->order('date asc')->field('post_id,post_title,date,timelen,hit')->select();
        //print_r($result);
        $hit=array();
        $timelen=array();
        $date=array();
        $i = 0;
        foreach ($result as $r) {
            if ($i == 0) {
               $_date = $r['date'];
            } else {
               $_date = date("Y-m-d",strtotime("$_date +1 day"));
                while ($r['date']!=$_date) {
                     array_push($hit,0);
                     array_push($timelen,0);
                     array_push($date,$_date);
                     $_date = date("Y-m-d",strtotime("$_date +1  day"));
                }
            }
            $i++;
            array_push($hit,$r['hit']);
            array_push($timelen,$r['timelen']);
            array_push($date,$r['date']);
        }
        //print_r($date);
        //print_r($timelen);
        $graph = new \Graph(600,400);
        $graph->SetScale('textint');
        $graph->SetYScale(0,'int');
        $graph->graph_theme = null;
        $graph->SetShadow();
        
        $lplot1 = new \LinePlot($hit);
        //$lplot1->value->Show();
        //$lplot1->mark->SetType(MARK_SQUARE);
        $lplot1->value->SetFormat('%d');
        //$lplot1->SetImpuls();        
        $lplot1->SetColor("#6495ED");
        //$lplot1->SetFillColor("#6495ED");
        $lplot1->SetLegend('HITs');
        $graph->Add($lplot1);
        $lplot2 = new \LinePlot($timelen);
        //$lplot2->value->Show();
        //$lplot2->SetImpuls();
        //$lplot2->mark->SetType(MARK_SQUARE);
        $lplot2->value->SetFormat('%d');
        $lplot2->SetColor("#FF1493");
        //$lplot2->SetFillColor("#FF1493");
        $lplot2->SetLegend('TIMEval(s)');
        $graph->AddY(0,$lplot2);
        
        $graph->img->SetMargin(70,70,70,70);
        $graph->xaxis->SetTickLabels($date);
        $graph->xaxis->SetTextLabelInterval(ceil(count($date)/13));
        $graph->xaxis->SetLabelAngle(30);
        $graph->legend->SetPos(0.05,0.05); 
        //$graph->yaxis->title->Set(iconv("UTF-8","GB2312//IGNORE","击点"));
        //$graph->yaxis->title->SetFont(FF_SIMSUN,FS_BOLD,10);
        //$graph->ynaxis[0]->title->SetFont(FF_SIMSUN,FS_BOLD,10);
        $graph->ynaxis[0]->SetColor('#FF1493');
        $graph->yaxis->SetColor('#6495ED');
        //$graph->ynaxis[0]->title->Set(iconv("UTF-8","GB2312//IGNORE","长时"));
        //$graph->yaxis->title->SetMargin(20);
        //$graph->ynaxis[0]->title->SetMargin(20);
        $graph->ynaxis[0]->HideTicks();
        $graph->img->SetAntiAliasing();
        $graph->Stroke();
    }
    function api(){
        //echo LIB_PATH;
        require_once(LIB_PATH.'Vendor/jpgraph/jpgraph.php');
        require_once(LIB_PATH.'Vendor/jpgraph/jpgraph_bar.php');
        $post_model=M('api');
    //$lists = sp_sql_posts("order:post_hits DESC;");
        $result=$post_model->order('freq desc')->limit(5)->field('uri,freq')->select();
        //print_r($result);
        $freq=array();
        $uri=array();
        foreach ($result as $r) {
            array_push($freq,(int)$r['freq']);
            array_push($uri,$r['uri']);
        }

		// Create the graph. These two calls are always required
        $graph = new \Graph(500,400); 
		$graph->SetScale("textint");
        $graph->graph_theme = null;
        // Add a drop shadow
        $graph->SetShadow();

        // Create a bar pot
        $bplot = new \BarPlot($freq);
        $bplot->value->Show();
        $graph->Add($bplot);
        $graph->xaxis->SetTickLabels($uri);
        $graph->img->SetMargin(50,50,50,60);
        
		$graph->title->Set(iconv("UTF-8","GB2312//IGNORE","api调用次数"));
		$graph->xaxis->title->Set(iconv("UTF-8","GB2312//IGNORE","URI"));
		$graph->yaxis->title->Set(iconv("UTF-8","GB2312//IGNORE","次数"));
		$graph->title->SetFont(FF_SIMSUN,FS_NORMAL);
		$graph->yaxis->title->SetFont(FF_SIMSUN,FS_BOLD);
		$graph->xaxis->title->SetFont(FF_SIMSUN,FS_BOLD);
        $graph->xaxis->SetFont(FF_SIMSUN,FS_BOLD,10);//坐标柱上的字体
		$graph->yaxis->SetFont(FF_SIMSUN,FS_BOLD,8);
        $graph->xaxis->title->SetMargin(15);//距离坐标轴的距离
		$graph->yaxis->title->SetMargin(15);//距离Y轴的距离
        $bplot->value->SetFormat('%d'); //值的格式化
        //$graph->xaxis->SetLabelAngle(30);
        //$graph->yaxis->scale->SetGrace(20); 
        
		// Display the graph
        $graph->Stroke();
	}
	function test(){
        //echo LIB_PATH;
        require_once(LIB_PATH.'Vendor/jpgraph/jpgraph.php');
        require_once(LIB_PATH.'Vendor/jpgraph/jpgraph_bar.php');
        $post_model=M('Posts');
    //$lists = sp_sql_posts("order:post_hits DESC;");
        $result=$post_model->order('post_hits desc')->limit(5)->field('post_title,post_hits')->select();
        //print_r($result);
        $hits=array();
        $title=array();
        foreach ($result as $r) {
            array_push($hits,(int)$r['post_hits']);
            array_push($title,iconv("UTF-8","GB2312//IGNORE",$r['post_title']));
        }

		// Create the graph. These two calls are always required
        $graph = new \Graph(500,400); 
		$graph->SetScale("textint");
        $graph->graph_theme = null;
        // Add a drop shadow
        $graph->SetShadow();

        // Create a bar pot
        $bplot = new \BarPlot($hits);
        $bplot->value->Show();
        $graph->Add($bplot);
        $graph->xaxis->SetTickLabels($title);
        $graph->img->SetMargin(50,50,50,60);
        
		$graph->title->Set(iconv("UTF-8","GB2312//IGNORE","最高点击"));
		$graph->xaxis->title->Set(iconv("UTF-8","GB2312//IGNORE","名称"));
		$graph->yaxis->title->Set(iconv("UTF-8","GB2312//IGNORE","点击"));
		$graph->title->SetFont(FF_SIMSUN,FS_NORMAL);
		$graph->yaxis->title->SetFont(FF_SIMSUN,FS_BOLD);
		$graph->xaxis->title->SetFont(FF_SIMSUN,FS_BOLD);
        $graph->xaxis->SetFont(FF_SIMSUN,FS_BOLD,10);//坐标柱上的字体
		$graph->yaxis->SetFont(FF_SIMSUN,FS_BOLD,8);
        $graph->xaxis->title->SetMargin(15);//距离坐标轴的距离
		$graph->yaxis->title->SetMargin(15);//距离Y轴的距离
        $bplot->value->SetFormat('%d'); //值的格式化
        //$graph->xaxis->SetLabelAngle(30);
        //$graph->yaxis->scale->SetGrace(20); 
        
		// Display the graph
        $graph->Stroke();
	}
	
	
}